jQuery(document).ready(function() {

	resizeIframe();
	jQuery(window).on("resize", function(event){
		resizeIframe();
		//fixWhiteSpaceIE11();
		jQuery("#mobile_number").blur();
	});
	//jQuery("#mobile_number").focus(); 
    
	jQuery('#mobile_number, #verif_code').bind("cut copy paste",function(e) {
     	 e.preventDefault();
  	});	
	
	if(/msie/.test(navigator.userAgent.toLowerCase())){
		if(navigator.appVersion.indexOf("MSIE 8.")!=-1 || navigator.appVersion.indexOf("MSIE 9.")!=-1){
			if (!$.support.placeholder) {
				 $("[placeholder]").focus(function () {
					 if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
				 }).blur(function () {
					 if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
				 }).blur();

				 $("[placeholder]").parents("form").submit(function () {
					 $(this).find('[placeholder]').each(function() {
						 if ($(this).val() == $(this).attr("placeholder")) {
							 $(this).val("");
						 }
					 });
				 });
				jQuery("#mobile_number").css("color","gray");
			}
			jQuery("#mobile_number").keypress(function() {				
				var $mobtel = jQuery("#mobile_number").val();
				if (jQuery.trim($mobtel).length > -1) {
					jQuery("#mobile_number").css("color","black");
				} else {
					jQuery("#mobile_number").css("color","gray");
				}
			}); 
			
			jQuery("#mobile_number").blur(function() {				
				if($(this).val() == $(this).attr('placeholder')) {	
					jQuery("#mobile_number").css("color","gray");
				} else {
					jQuery("#mobile_number").css("color","black");
				}
			}); 
		}
	}
	
	/*Hide Div*/
	jQuery("#verificationCode, .errorBox2").hide();	
	
	var current_pos = getCookie("tiangge_current_pos");
	if (current_pos > 0) {
		//console.log(current_pos + "TRUE");
		if(current_pos=="1") {
		jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="2") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="3") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
		
		  var mode = getCookie("tiangge_mode");
		  var promo = getCookieR("tiangge_promo");
		  var promoDesc = getCookieR("tiangge_promoDesc");
		  var commitmentBox = getCookieR("tiangge_commitmentBox");
		  var mobtel = getCookie("tiangge_mobtel");
		  if(mobtel=="0"){jQuery('#mobtel').text("None");} else {jQuery('#mobtel').text(mobtel);}
          if(mode!="edit"){    
			jQuery(".promoBox, .commitmentBox, .prepaidBox").addClass("active");
			jQuery('#pname, #pemail, #pdob, #padd').hide();
			if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
				promoDesc = promoDesc.replace("john", ",");
				promoDesc = promoDesc.replace("john ", ", ");
				jQuery('#promo').html(promo + promoDesc);
			} else {
				jQuery('#promo').html(promo + promoDesc);
			}
			jQuery('#commitmentBox').text(commitmentBox);
			if(mobtel=="0"){jQuery('#mobtel').text("None");} else {jQuery('#mobtel').text(mobtel);}
		  }
		}
		if(current_pos=="4" || current_pos=="5") {
		jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
		
		  var mode = getCookie("tiangge_mode");
		  var promo = getCookieR("tiangge_promo");
		  var promoDesc = getCookieR("tiangge_promoDesc");
		  var commitmentBox = getCookieR("tiangge_commitmentBox");
		  var mobtel = getCookie("tiangge_mobtel");
		  var username = getCookieR("tiangge_username");
		  var eadd = getCookieR("tiangge_eadd");
		  var bday = getCookieR("tiangge_bday");
		  var address = getCookieR("tiangge_address");
		  if(mobtel=="0"){jQuery('#mobtel').text("None");} else {jQuery('#mobtel').text(mobtel);}
          if(mode!="edit"){ 
			jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox").addClass("active");
			jQuery(".pnone").hide();
			if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
				promoDesc = promoDesc.replace("john", ",");
				promoDesc = promoDesc.replace("john ", ", ");
				jQuery('#promo').html(promo + promoDesc);
			} else {
				jQuery('#promo').html(promo + promoDesc);
			}
			jQuery('#commitmentBox').text(commitmentBox);
			if(mobtel=="0"){jQuery('#mobtel').text("None");} else {jQuery('#mobtel').text(mobtel);}
			jQuery('#name').text(username);
			jQuery('#email').text(eadd);
			jQuery('#dob').text(bday);
			jQuery('#add').text(address);
			jQuery("#pname, #pemail, #pdob, #padd").show();
		  }
		}
	} else {
		//alert(current_pos + "FALSE");
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	} 
	
	
	//called when key is pressed in numberfield
	/*jQuery("#mobile_number, #verif_code").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
		return false;
		}
	});	
	*/
	jQuery("#mobile_number, #verif_code").keypress(function (evt) {
	
	  	 evt = evt || window.event;
  		 var charCode = evt.which || evt.keyCode;
  		 var charStr = String.fromCharCode(charCode);
  		 if ( /[a-z0-9]/i.test(charStr) || evt.which == 8 || evt.which == 9 || (evt.which > 36 || evt.which < 41)) {
  		 }else{
			return false;
		 }
		 
		 if (evt.which != 8 && evt.which != 0 && (evt.which < 48 || evt.which > 57)) {
            return false;
    	 }
	}); 

	//Number only Note 3
	jQuery('#mobile_number, #verif_code').keyup(function () {
		if (this.value != this.value.replace(/[^0-9]/g, '')) {
		  this.value = this.value.replace(/[^0-9]/g, '');
		}
	});
	
	jQuery('#mobile_number').keypress(function (e) {
	jQuery(".error1, .error2, .error3, .error4, .errorBox2, #verificationCode").hide();	
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#submit_mobtel").click();
	  }
	});
	
	jQuery('#verif_code').keypress(function (e) {
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#validate_verif_code").click();
	  }
	});
	
	jQuery("#submit_mobtel").click(function(){
		mobtel=jQuery("#mobile_number").val();
		var current_num = getCookie("tiangge_mobtel");

		if(mobtel.length!=11 || mobtel=="09xxxxxxxxx") {
			jQuery("#submit_mobtel").text("Send code");
			jQuery(".error1").show();
			jQuery(".error2, .error3, .error4").hide();
			jQuery("#verificationCode").hide();
			jQuery("#mobile_number").addClass("error");
			jQuery("#mobile_number").focus();
		} else if (current_pos > 3 && current_num == mobtel){
			jQuery(".error1, .error3, .error4").hide();
			jQuery(".error2").show();
			jQuery("#verificationCode").hide();
			jQuery("#mobile_number").addClass("error");
			jQuery("#verificationCode").hide();
			jQuery("#mobile_number").focus();
		} else {
			jQuery("#prepaidNumber .errorBox").hide();
			
			jQuery.getJSON(baseurl + "prepaid/verifynumber/" + mobtel,{ajax: 'true'}, function(j){
				if(j.status==0) {
					jQuery("#submit_mobtel").text("Resend code");			
					jQuery(".error1, .error2, .error3, .error4, .errorBox2").hide();
					jQuery("#verif_code").val("");
					jQuery("#verif_code").removeClass("error");
					jQuery("#verificationCode").show();
					jQuery("#mobile_number").removeClass("error");
					jQuery("#verif_code").focus(); 
					if ("window", $( window ).width() < 361) {
						scrollFrameHeight('200');	
					} else if ("window", $( window ).width() < 641) {
						scrollFrameHeight('400');
					}
				} else if (j.status==-3){
				    jQuery(".error1, .error2, .error4").hide();
					jQuery(".error3").show();
					jQuery("#mobile_number").addClass("error");
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number").focus();
				} else if (j.status==-2){
				    jQuery(".error1, .error2, .error3").hide();
					jQuery(".error4").show();
					jQuery("#mobile_number").addClass("error");
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number").focus();
				} else {
					jQuery(".error2, .error3, .error4").hide();
					jQuery(".error1").show();
					jQuery("#mobile_number").addClass("error");
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number").focus();
				}	
				resizeIframe();	
			});
		}
	});
	
	jQuery("#noneGlobe").click(function() {
		var current_pos = getCookie("tiangge_current_pos");
		if (current_pos < 3) {
			setCookie("tiangge_current_pos","3");
		}
	});
	
	jQuery("#validate_verif_code").click(function() {
		vcode=jQuery("#verif_code").val();
		if(vcode.length==6) {
			jQuery("#verificationCode .errorBox2").hide();
			
			jQuery.getJSON(baseurl + "prepaid/verifycode/" + vcode,{ajax: 'true'}, function(j){
				if(j.status==0) {
					var mobtel = jQuery("#mobile_number").val(); 
					jQuery("#verif_code").removeClass("error");
					setCookie("tiangge_mobtel",mobtel);
					var current_pos = getCookie("tiangge_current_pos");
					if (current_pos < 3) {
						setCookie("tiangge_current_pos","3");
					}
					setCookie("tiangge_usertype","globe");
					document.cookie = 'temp_mobtel=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
					window.location=j.redirect;
				} else {
					jQuery("#verificationCode .errorBox2").show();
					jQuery("#verif_code").addClass("error");
					jQuery(verif_code).focus();
				}	
			});
		} else {
			jQuery("#verificationCode .errorBox2").show();
			jQuery("#verif_code").addClass("error");
		}
	});

	/*. . . Add underline to a.linkColor. . . */
	$("#prepaidNumber p a.linkColor").attr("style","text-decoration:underline;");
	/*. . . Add underline to a.linkColor. . . */

	if(/prepaid/.test(self.location.href)){
	   var mode = getCookie("tiangge_mode");
	   var promo = getCookieR("tiangge_promo");
	   /* var callDesc = getCookieR("tiangge_callDesc");
		  var textDesc = getCookieR("tiangge_textDesc");
		  var surfDesc = getCookieR("tiangge_surfDesc");
		  cookiestring = "";
			if(textDesc!=="") cookiestring += textDesc;
			if(callDesc !==null) cookiestring += ", " + callDesc;
			if(surfDesc!=="") cookiestring += " and " + surfDesc; */
	   var promoDesc = getCookieR("tiangge_promoDesc");
	   var commitmentBox = getCookieR("tiangge_commitmentBox");
	   var mobtel = getCookie("tiangge_mobtel");
	   var username = getCookieR("tiangge_username");
	   var eadd = getCookieR("tiangge_eadd");
	   var bday = getCookieR("tiangge_bday");
       var address = getCookieR("tiangge_address");
	   var area = getCookieR("tiangge_area");
	   var town = getCookieR("tiangge_town");
	   var province = getCookieR("tiangge_province");
	   var postalcode = getCookieR("tiangge_postalcode");
       jQuery("p.pnone").hide();
       if(mode=="edit"){
		 jQuery(".promoBox").addClass("active");
		 if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
			promoDesc = promoDesc.replace("john", ",");
			promoDesc = promoDesc.replace("john ", ", ");
			jQuery('#promo').html(promo + promoDesc);
		 } else {
			jQuery('#promo').html(promo + promoDesc);
		 }
		 jQuery(".commitmentBox").addClass("active");
		 jQuery('#commitmentBox').text(commitmentBox);
		 jQuery(".prepaidBox").addClass("active");
		 //jQuery('#mobtel').text(mobtel);
		 if(mobtel=="0"){jQuery('#mobtel').text("None");} else {jQuery('#mobtel').text(mobtel);}
	     jQuery(".pnone").hide();
	     jQuery(".profileBox").addClass("active");
		 jQuery('#name').text(username);
		 jQuery('#email').text(eadd);
		 jQuery('#dob').text(bday);

		 if(province=="Metro Manila") 
			var address = address + ', ' + area + ', ' + town + ', ' + province + ', ' + postalcode;
		 else
			var address = address + ', ' + town + ', ' + province + ', ' + postalcode;
				
		 jQuery('#add').text(address);
		 jQuery("#pname").show();
		 jQuery("#pemail").show();
		 jQuery("#pdob").show();
		 jQuery("#padd").show();
       }
    }
	
	/* jQuery(function(){
	var $html=$("html");
	if(((window.innerWidth==768&&window.innerHeight==927)||(window.innerWidth==1024&&window.innerHeight==671))){
		$(window).on('resize', function(){
			$('#mobile_number',$html).blur();
		});
	}
	}); */
});

   //Create Cookie
function setCookie(cname,cvalue) {
    var d = new Date();
    d.setTime(d.getTime() + (30*60*1000));
	
	ua_tab2ics = "IMM76D";
	_ua = navigator.userAgent;
	
	if(_ua.search(ua_tab2ics) > 0)
	    document.cookie = cname+"="+cvalue+"; path=/"; 
	else{
	
	var expiration = getCookie("tiangge_expiration");
	if(expiration=="")
		var expires = "expires=" + d.toUTCString();
	else{
		var e = new Date();
		e.setTime(parseInt(expiration) + (30*60*1000));
	    var expires = "expires=" + e.toUTCString();
	}
	
	//document.cookie = cname+"="+cvalue+"; path=/;"+expires;
	document.cookie = cname+"="+cvalue+"; path=/";
	}
}

   //Retrieve Cookie
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
}
   
   //Retrieve Cookie
function getCookieR(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function fixWhiteSpaceIE11(){
	var trident = !!navigator.userAgent.match(/Trident\/7.0/);
	var net = !!navigator.userAgent.match(/.NET4.0E/);
	var IE11 = trident && net
	var IEold = ( navigator.userAgent.match(/MSIE/i) ? true : false );
	if(IE11){
		console.log("IE");
		window.parent.postMessage(800,"*"); 
		jQuery(".width960c").attr("style","height:734px;");
	}
 }

